//
//  EvaluateTaskViewController.m
//  OAProject
//
//  Created by yhj on 2017/4/5.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  评价任务主界面

#import "EvaluateTaskViewController.h"
#import "TaskListViewController.h"

@interface EvaluateTaskViewController ()

@property(nonatomic,strong)UIView *bgView;

// 任务评分
@property(nonatomic,strong)UILabel *titleLabel;

// 质量
@property(nonatomic,strong)XHStarRateView *qualityRateView;
@property(nonatomic,strong)NSString *qualityRateStore;

// 效率
@property(nonatomic,strong)XHStarRateView *levelRateView;
@property(nonatomic,strong)NSString *levelRateStore;

// 态度
@property(nonatomic,strong)XHStarRateView *attitudeRateView;
@property(nonatomic,strong)NSString *attitudeRateStore;

// 留言
@property(nonatomic,strong)UITextView *rateTextView;

@end

@implementation EvaluateTaskViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"评价任务";

    self.automaticallyAdjustsScrollViewInsets=NO;

    [self.view addSubview:self.bgView];

    [AppUI initNavigationItem:self.navigationItem andWithText:@"提  交" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        if (self.qualityRateStore ==nil)
        {
            [self.view makeToast:@"请对任务质量进行评价" duration:1 position:CSToastPositionCenter];
        }
        else if (self.levelRateStore==nil)
        {
            [self.view makeToast:@"请对任务效率进行评价" duration:1 position:CSToastPositionCenter];
        }
        else if (self.attitudeRateStore==nil)
        {
            [self.view makeToast:@"请对任务态度进行评价" duration:1 position:CSToastPositionCenter];
        }
        else
        {
            [API EvaluateTaskWithTaskID:self.model.taskID quality:self.qualityRateStore efficient:self.levelRateStore attitude:self.attitudeRateStore memo:self.rateTextView.text step_evaluate:@[] success:^(id res) {

                if ([res[@"rt"] isEqual:@(1)])
                {
                    //              [self.view makeToast:@"评价成功" duration:1 position:CSToastPositionCenter];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                        // 返回到任务列表界面
                        for (UIViewController *vc in self.navigationController.viewControllers)
                        {
                            if ([vc isKindOfClass:[TaskListViewController class]])
                            {
                                [self.navigationController popToViewController:vc animated:NO];
                            }
                        }
                    });
                }
                else
                {
                    [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                }
                
            } failure:^(NSInteger stateCode) {
                
            }];
        }
   }];
}

-(UIView *)bgView
{
    if (!_bgView) {
        _bgView=[[UIView alloc]initWithFrame:CGRectMake(0,64,APPW,0)];
        _bgView.backgroundColor=WhiteColor;
        setViewHeight_Add(_bgView,kmargin);

        // 任务评价
        UILabel *label=[self.bgView getLabelWithText:@"任务评价" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:label];
        label.frame=CGRectMake(kmargin*2,_bgView.frame.size.height,APPW/2,30);
        setViewHeight_Add(_bgView,kmargin*3);


        UILabel *qualityLabel=[self.bgView getLabelWithText:@"质      量" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:qualityLabel];
        qualityLabel.frame=CGRectMake(kmargin*2,self.bgView.frame.size.height,APPW/4-kmargin*2,kmargin*3);

        _qualityRateView=[[XHStarRateView alloc]initWithFrame:CGRectMake(APPW/4,self.bgView.frame.size.height+kmargin/2,APPW/2,kmargin*2) numberOfStars:5 rateStyle:WholeStar isAnination:YES finish:^(CGFloat currentScore) {

            self.qualityRateStore=[NSString stringWithFormat:@"%.0f",currentScore];

        }];
        [self.bgView addSubview:_qualityRateView];
        setViewHeight_Add(_bgView,kmargin*3);

        UILabel *levelLabel=[self.bgView getLabelWithText:@"效      率" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:levelLabel];
        levelLabel.frame=CGRectMake(kmargin*2,self.bgView.frame.size.height,APPW/4-kmargin*2,kmargin*3);

        _levelRateView=[[XHStarRateView alloc]initWithFrame:CGRectMake(APPW/4,self.bgView.frame.size.height+kmargin/2,APPW/2,kmargin*2) numberOfStars:5 rateStyle:WholeStar isAnination:YES finish:^(CGFloat currentScore) {

            self.levelRateStore=[NSString stringWithFormat:@"%.0f",currentScore];

        }];
        [self.bgView addSubview:_levelRateView];
        setViewHeight_Add(_bgView,kmargin*3);

        UILabel *attitudeLabel=[self.bgView getLabelWithText:@"态      度" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:attitudeLabel];
        attitudeLabel.frame=CGRectMake(kmargin*2,self.bgView.frame.size.height,APPW/4-kmargin*2,kmargin*3);

        _attitudeRateView=[[XHStarRateView alloc]initWithFrame:CGRectMake(APPW/4,self.bgView.frame.size.height+kmargin/2,APPW/2,kmargin*2) numberOfStars:5 rateStyle:WholeStar isAnination:YES finish:^(CGFloat currentScore) {

        self.attitudeRateStore=[NSString stringWithFormat:@"%.0f",currentScore];

        }];
        [self.bgView addSubview:_attitudeRateView];
        setViewHeight_Add(_bgView,kmargin*3);

        UILabel *memoLabel=[self.bgView getLabelWithText:@"留      言" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:memoLabel];
        memoLabel.frame=CGRectMake(kmargin*2,self.bgView.frame.size.height,APPW/4-kmargin*2,kmargin*3);

        _rateTextView=[UITextView new];
        _rateTextView.font=Font_Number(16);
//        ViewBorderRadius(_rateTextView,kmargin/2,1,Color999999);
        _rateTextView.placeholder=@"说说我的任务评价吧";
        _rateTextView.frame=CGRectMake(APPW/4,self.bgView.frame.size.height+kmargin/2,APPW*3/4-kmargin*2,kmargin*6);
        [self.bgView addSubview:_rateTextView];
        _rateTextView.textColor=DarkGreyColor;
         setViewHeight_Add(_bgView,kmargin*8);
    }
    return _bgView;
}

@end
